package com.zjsru.leetcode75.level1;

/**
 * @Author: cookLee
 * @Date: 2023-11-07
 * 第 N 个泰波那契数
 * T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2
 */
public class Tribonacci {

    /**
     * 主
     * \
     * 输入：n = 4
     * 输出：4
     * 解释：
     * T_3 = 0 + 1 + 1 = 2
     * T_4 = 1 + 1 + 2 = 4
     * \
     * @param args args
     */
    public static void main(String[] args) {
        Tribonacci tribonacci = new Tribonacci();
        int n = 4;
        System.out.println(tribonacci.tribonacci(n));
    }

    /**
     * 模拟
     *
     * @param n n
     * @return int
     */
    public int tribonacci(int n) {
        if(n == 0){
            return 0;
        }
        if(n <= 2){
            return 1;
        }
        int ans = 1;
        int firstStep = 0;
        int secondStep = 0;
        int thirdStep = 1;
        //直接从第三位开始模拟
        for(int i = 3; i <= n;i++){
            firstStep = secondStep;
            secondStep = thirdStep;
            thirdStep = ans;
            ans = firstStep + secondStep + thirdStep;
        }

        return ans;
    }

}
